from lin_base import *

def build_ecl_ras(year,tz):
    try:
        Obl = g_share.Obl
    except:
        from g_share import g_share
        nd = cal_d_w_hm(year,1,1,-tz,0)
        Obl = g_share.Obl
    try:
        ecl_ras = g_share.ecl_ras
    except:
        ecl_ras=[]
    if len(ecl_ras)==0:
        ecl_long=0
        ecl_lat=0
        for j in range(360):
            ecl_long =j
            ra, dec = radc(ecl_long, ecl_lat)
            ecl_ras.append(ra)
        g_share.ecl_ras = ecl_ras    

def add_lin_ecl(paper,title,x0,y0,xend,font=unicode_font_36,
                    fgcolor=BLACK,lw=2):
    layer=paper.add_layer(name='ecl')
    draw = layer.draw
    draw.text((x0-XM_TITLE,y0+YM_TEXT),title, font=font,fill=fgcolor)
    ecl_lat=0
    pra=None
    ecl_deg_start=ECL_START * 15
    for ecl_long in range(ecl_deg_start,ECL_TOTAL *15): #49*15):
        ecl_deg= ecl_long % 360
        if True:
            ra, dec = radc(ecl_long, ecl_lat)
            ra_hr = ra/15
            #print('ecl_long:%s ra_hr:%s' % (ecl_long, ra_hr))
            if pra is None:
                while True:
                    x=RA_to_x(x0,ra_hr)
                    if x >=x0:
                        break
                    else:
                        ra_hr += 24
            if pra is not None:
                while ra_hr < pra:
                    ra_hr += 24
            pra = ra_hr
            x= RA_to_x(x0, ra_hr)
            if x > g_share.xr_end:
                break
            if (ecl_deg % 10)==0:
                draw.line([(x,y0),(x,y0+YM_LONG)],fill=fgcolor,width=lw)  
                if (ecl_deg % 30)==0:
                    draw.text((x,y0+YM_TEXT),'%d' % ecl_deg , font=font,fill=fgcolor)
            else:
                draw.line([(x,y0),(x,y0+YM_SHORT)],fill=fgcolor,width=lw)
                
    draw.line([(x0,y0),(xend,y0)],fill=BLACK,width=2)

def add_lin_ecl_hm(paper,title,x0,y0,xend,font=unicode_font_36,
                    fgcolor=BLACK,lw=2):
    layer=paper.add_layer(name='ecl')
    draw = layer.draw
    draw.text((x0-XM_TITLE,y0+YM_TEXT),title, font=font,fill=fgcolor)
    ecl_lat=0
    pra=None
    for hx in range(ECL_START, ECL_TOTAL):
        h = hx % 24
        for m in [0,10,20,30,40,50]:
            ecl_long = (h + m/60) * 15
            ra, dec = radc(ecl_long, ecl_lat)
            ra_hr = ra/15
            print('ecl_long:%s h:%s m:%s ra_hr:%s' % (ecl_long,h,m, ra_hr))
            if pra is None:
                while True:
                    x=RA_to_x(x0,ra_hr)
                    if x >=x0:
                        break
                    else:
                        ra_hr += 24
            if pra is not None:
                while ra_hr < pra:
                    ra_hr += 24
            pra = ra_hr
            x= RA_to_x(x0, ra_hr)
            if m==0:
                draw.line([(x,y0),(x,y0+YM_LONG)],fill=fgcolor,width=lw)  
                draw.text((x,y0+YM_TEXT),'%02d' % h , font=font,fill=fgcolor)
            else:
                draw.line([(x,y0),(x,y0+YM_SHORT)],fill=fgcolor,width=lw)
                
    draw.line([(x0,y0),(xend,y0)],fill=BLACK,width=2)